/*
 * File:     controllerwidget.cpp
 * Author:   Thomas Bruederli
 *
 * Implementation of the QWidget to display the status of the wii controller.
 *
 * This file is part of the Qt-based cross-platform Wii device driver.
 *
 * Copyright (C) 2009-2010, Dominic Stalder & Thomas Bruederli
 * Licensed under the GNU Lesser General Public License (LGPL) as published
 * by the Free Software Foundation at http://www.gnu.org/licenses/lgpl-3.0.txt
 *
 * $Id: controllerwidget.cpp 5 2010-03-03 09:10:12Z roundcube $
 */

#include <iostream>
#include <QtGui>
#include "controllerwidget.h"
#include "wiidevicedriver.h"

ControllerWidget::ControllerWidget(QWidget *parent) : QWidget(parent)
{
	resize(300, 280);
	setMinimumSize(300, 280);

	bgimage = new QLabel(this);
	bgimage->setPixmap(QPixmap(":/res/wiicontroller.png"));
	bgimage->setGeometry(70, 60, 142, 210);
	
	statusLabel = new QLabel(this);
    statusLabel->setGeometry(10, 20, 270, 20);
    QFont font;
    font.setBold(true);
    font.setWeight(75);
    statusLabel->setFont(font);
    statusLabel->setAlignment(Qt::AlignCenter|Qt::AlignVCenter);
	
	// TODO: don't use the green dot for visualization
	marker = new QLabel(this);
	marker->setPixmap(QPixmap(":/res/greendot.png"));
	marker->setGeometry(0, 0, 24, 24);
	marker->setVisible(0);
	
	// create text fields for acceleration values
	QLabel *label2 = new QLabel(this);
	label2->setText(tr("Roll:"));
    label2->setGeometry(10, 280, 40, 20);
    label2->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
	
	accXLabel = new QLineEdit(this);
    accXLabel->setGeometry(55, 280, 60, 22);
    accXLabel->setAlignment(Qt::AlignCenter);
    accXLabel->setReadOnly(true);
	
	QLabel *label3 = new QLabel(this);
	label3->setText(tr("Pitch:"));
    label3->setGeometry(130, 280, 40, 20);
    label3->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
	
	accYLabel = new QLineEdit(this);
    accYLabel->setGeometry(175, 280, 60, 22);
    accYLabel->setAlignment(Qt::AlignCenter);
    accYLabel->setReadOnly(true);
}

/**
 * Slot for instruction signals
 */
void ControllerWidget::setInstructions(int, const QString &msg, bool)
{
	statusLabel->setText(msg);
}

/**
 * Slot for button changed signals
 */
void ControllerWidget::buttonChanged(int which, int on)
{
	int x, y;
	int offset_x = 10 - 12;
	int offset_y = 60 - 12;
	
	// TODO: find a better way to visualize button states
	switch (which)
	{
		case WiiDeviceDriver::BUTTON_A: x = 90; y = 66; break;
		case WiiDeviceDriver::BUTTON_B: x = 168; y = 46; break;
		default: x = -30; y = -30; break;
	}
	
	marker->setGeometry(x + offset_x, y + offset_y, 24, 24);
	marker->setVisible(on);
}

/**
 * Slot for button acceleration values
 */
void ControllerWidget::orientationChanged(double roll, double pitch, double yaw)
{
	accXLabel->setText(QString("%1").arg(roll, 0, 'f', 0));
	accYLabel->setText(QString("%1").arg(pitch, 0, 'f', 0));
}
